#include "SystemAllocation.h"


SystemAllocation::SystemAllocation(){
	response_times = 0;
	extent_size = 3.75;
	SystemGroups = 0;
}

SystemAllocation::~SystemAllocation(){
	map<double, list<Lun*>*>::iterator it;
	for (it = SystemGroups->begin(); it != SystemGroups->end(); it++){
		delete it->second;
	}
	delete SystemGroups;
}

void SystemAllocation::setGroupsPercentsNumOfLuns(double gp1, double gp2,double gp3,int n_luns){
	group_percents[0] = gp1;
	group_percents[1] = gp2;
	group_percents[2] = gp3;
	nof_luns = n_luns;
}

int SystemAllocation::setRandomResponseTime(Lun* l){
	if (response_times->size() != 0){
		int nof_groups = response_times->size();
		int count = 0, random = ((rand()-1) % nof_groups);
		map<double, list<Lun*>*>::iterator it;
		while ((count == 0) || (it->second->size() > (nof_luns*group_percents[random]))){
			if (count == nof_groups){
				nof_luns++;
				cout << " not enough luns given, adding one more: " << nof_luns << endl;
			}
			random = ((random+1) % nof_groups);
			it = SystemGroups->find(response_times->at(random));
			if (it == SystemGroups->end()){
				cout << "ERROR: could not find Lun Group" << endl;
				return -1;
			}
			count++;
		}
		l->setDRT(response_times->at(random));
		l->setPriorityGroup(random+1);
		it->second->push_back(l);
		return 0; 
	}
	else {
		cout << "ERROR: no response times available" << endl;
		return -2;
	}
}

void SystemAllocation::setResponseTimes(vector<double>* r_t){
	response_times = r_t;
	SystemGroups = new map<double, list<Lun*>*>();
	for (int i = 0; i < r_t->size(); i++){
		SystemGroups->insert(pair<double, list<Lun*>*>(response_times->at(i), new list<Lun*>()));
	}
}

void SystemAllocation::setSSD_size(DeviceData* device , double devCount){
	SSD_size = device->setSize(devCount);
}

void SystemAllocation::setFC_size(DeviceData* device, double devCount){
	FC_size = device->setSize(devCount);
}

void SystemAllocation::setSATA_size(DeviceData* device, double devCount){
	SATA_size = device->setSize(devCount);
}

void SystemAllocation::setSystem_cost(int cost){
	system_cost = cost;
}

void SystemAllocation::setFix_sata(int sata){
	fix_sata = sata;
}
